<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="地区编号" prop="areaId">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入地区编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobiles">
        <el-input
          v-model="queryParams.mobiles"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="门店状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择门店状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.STORE_SHOP_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['store:shop:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['store:shop:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" fixed="left" />
      <el-table-column label="门店名称" align="center" prop="name" fixed="left" width="150px" />
      <el-table-column label="所在地区" align="center" prop="areaId" width="180px">
        <template #default="scope">{{scope.row.areaName}}</template>
      </el-table-column>
      <el-table-column label="营业日期" align="center" prop="openingDate" />
      <el-table-column label="营业时间" align="center" prop="openingTime" width="150px" />
      <el-table-column label="门店联系电话" align="center" prop="mobiles" width="150px" />
      <el-table-column label="设施及服务" align="center" prop="services" width="150px" />
      <el-table-column label="门店地址" align="center" prop="address" width="150px" />
      <el-table-column label="门店点击量" align="center" prop="browseCount" />
      <el-table-column label="门店业务量" align="center" prop="businessCount" />
      <el-table-column label="门店坐标" align="center" prop="coordinate" />
      <el-table-column label="关注量" align="center" prop="concernCount" />
      <el-table-column label="评分" align="center" prop="star" />
      <el-table-column label="商品评分" align="center" prop="goodsStar" />
      <el-table-column label="体验评分" align="center" prop="explorationStar" />
      <el-table-column label="服务评分" align="center" prop="serviceStar" />
      <el-table-column label="人均消费" align="center" prop="perPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="门店状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.STORE_SHOP_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="入驻联系电话" align="center" prop="enterMobile" width="150px" />
      <el-table-column label="支付宝信息" align="center" prop="alipayName" width="150px">
        <template #default="scope">
          <el-text v-if="scope.row.alipayName">{{scope.row.alipayName}}</el-text>
          <el-text v-if="scope.row.alipayAccount">{{scope.row.alipayAccount}}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="负责人" align="center" prop="ownerUserId" />
      <el-table-column label="门店照片" align="center" prop="imgs" width="300px">
        <template #default="scope">
          <el-image :src="img" class="h-60px w-60px" @click="imagePreview(img)" v-for="img in scope.row.imgs" :key="img" />
        </template>
      </el-table-column>
      <el-table-column label="营业执照" align="center" prop="yyzz">
        <template #default="scope">
          <el-image :src="scope.row.yyzz" class="h-60px w-60px" @click="imagePreview(scope.row.yyzz)" />
        </template>
      </el-table-column>
      <el-table-column label="食品经营许可证" align="center" prop="spjyxkz">
        <template #default="scope">
          <el-image :src="scope.row.spjyxkz" class="h-60px w-60px" @click="imagePreview(scope.row.spjyxkz)" />
        </template>
      </el-table-column>
      <el-table-column label="娱乐经营许可证" align="center" prop="yljyxkz">
        <template #default="scope">
          <el-image :src="scope.row.yljyxkz" class="h-60px w-60px" @click="imagePreview(scope.row.yljyxkz)" />
        </template>
      </el-table-column>
      <el-table-column label="城市合伙人合作码" align="center" prop="cityCoopCode" />
      <el-table-column label="注销联系电话" align="center" prop="cancelMobile" />
      <el-table-column label="注销确认书" align="center" prop="cancelConfirmFile">
        <template #default="scope">
          <el-image :src="scope.row.cancelConfirmFile" class="h-60px w-60px" @click="imagePreview(scope.row.cancelConfirmFile)" />
        </template>
      </el-table-column>
      <el-table-column label="注销营业执照" align="center" prop="cancelBusinessFile">
        <template #default="scope">
          <el-image :src="scope.row.cancelBusinessFile" class="h-60px w-60px" @click="imagePreview(scope.row.cancelBusinessFile)" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus" width="100px" fixed="right">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_AUDIT_STATUS" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="180px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="warning"
            v-if="scope.row.auditStatus==0"
            @click="handleAudit(scope.row.id)"
            v-hasPermi="['store:shop:audit']"
          >
            审核
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['store:shop:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['store:shop:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ShopForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ShopApi, ShopVO } from '@/api/store/shop'
import ShopForm from './ShopForm.vue'
import { createImageViewer } from '@/components/ImageViewer'

/** 门店信息 列表 */
defineOptions({ name: 'Shop' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ShopVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  areaId: undefined,
  name: undefined,
  openingDate: [],
  openingTime: [],
  mobiles: undefined,
  services: undefined,
  address: undefined,
  browseCount: undefined,
  businessCount: undefined,
  star: undefined,
  goodsStar: undefined,
  explorationStar: undefined,
  serviceStar: undefined,
  perPrice: undefined,
  coordinate: undefined,
  concernCount: undefined,
  remark: undefined,
  status: undefined,
  createTime: [],
  deptId: undefined,
  ownerUserId: undefined,
  yyzz: undefined,
  spjyxkz: undefined,
  yljyxkz: undefined,
  cityCoopCode: undefined,
  cancelMobile: undefined,
  cancelConfirmFile: undefined,
  cancelBusinessFile: undefined,
  enterMobile:undefined,
  alipayName:undefined,
  alipayAccount:undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ShopApi.getShopPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    zIndex: 9999999,
    urlList: [imgUrl]
  })
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ShopApi.deleteShop(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/**
 * 审核按钮操作
 */
const handleAudit = async(id: number) => {
  try {
    //操作的二次确认
    await message.confirm("是否通过审核？")
    //发起审核
    await ShopApi.auditShop(id)
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ShopApi.exportShop(queryParams)
    download.excel(data, '门店信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
